Python ChatGPT Telegram Bot

注册

这里如何注册我就不说明了,大家自行去注册,主要是现在GPT的基本上已经备用很多了,导致了接码的价格也上涨了,而且使用token的话,其实还是很快可以用完免费的18美金;
接码:https://sms-activate.org/

准备材料

主要提供下Python的实现代码,首先需要准备一下的东西:

脚本:

main.py >folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 1. Start by importing the necessary libraries and setting up the API clients  
import requests
import json
import os
import threading

# OpenAI secret Key
API_KEY = 'xxxxxxxxx'
# Models: text-davinci-003,text-curie-001,text-babbage-001,text-ada-001
MODEL = 'text-davinci-003'
# Telegram secret access bot token
BOT_TOKEN = 'xxxxxxxxxxxxx'
# Defining the bot's personality using adjectives
BOT_PERSONALITY = ''


# 2a. Function that gets the response from OpenAI's chatbot
def openAI(prompt):
# Make the request to the OpenAI API
response = requests.post(
'https://api.openai.com/v1/completions',
headers={'Authorization': f'Bearer {API_KEY}'},
json={'model': MODEL, 'prompt': prompt, 'temperature': 0.4, 'max_tokens': 4000}
)

result = response.json()
print(result)
final_result = ''.join(choice['text'] for choice in result['choices'])
return final_result


# 2b. Function that gets an Image from OpenAI
def openAImage(prompt):
# Make the request to the OpenAI API
resp = requests.post(
'https://api.openai.com/v1/images/generations',
headers={'Authorization': f'Bearer {API_KEY}'},
json={'prompt': prompt, 'n': 1, 'size': '1024x1024'}
)
response_text = json.loads(resp.text)

return response_text['data'][0]['url']


# 3a. Function that sends a message to a specific telegram group
def telegram_bot_sendtext(bot_message, chat_id, msg_id):
data = {
'chat_id': chat_id,
'text': bot_message,
'reply_to_message_id': msg_id
}
response = requests.post(
'https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage',
json=data
)
return response.json()


# 3b. Function that sends an image to a specific telegram group
def telegram_bot_sendimage(image_url, group_id, msg_id):
data = {
'chat_id': group_id,
'photo': image_url,
'reply_to_message_id': msg_id
}
url = 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendPhoto'

response = requests.post(url, data=data)
return response.json()


# 4. Function that retrieves the latest requests from users in a Telegram group,
# generates a response using OpenAI, and sends the response back to the group.
def Chatbot():
# Retrieve last ID message from text file for ChatGPT update
cwd = os.getcwd()
filename = cwd + '/chatgpt.txt'
if not os.path.exists(filename):
with open(filename, "w") as f:
f.write("1")
else:
print("File Exists")

with open(filename) as f:
last_update = f.read()

# Check for new messages in Telegram group
url = f'https://api.telegram.org/bot{BOT_TOKEN}/getUpdates?offset={last_update}'
response = requests.get(url)
data = json.loads(response.content)
for result in data['result']:
try:
# Checking for new message
if float(result['update_id']) > float(last_update):
# Checking for new messages that did not come from chatGPT
if not result['message']['from']['is_bot']:
last_update = str(int(result['update_id']))

# Retrieving message ID of the sender of the request
msg_id = str(int(result['message']['message_id']))

# Retrieving the chat ID
chat_id = str(result['message']['chat']['id'])

# Checking if user wants an image
if '/img' in result['message']['text']:
prompt = result['message']['text'].replace("/img", "")
bot_response = openAImage(prompt)
print(telegram_bot_sendimage(bot_response, chat_id, msg_id))
# Checking that user mentionned chatbot's username in message
if '@chatGpt_dandelion_bot' in result['message']['text'] \
or '/ask' in result['message']['text']:
prompt = result['message']['text'].replace("@chatGpt_dandelion_bot", "")
# Calling OpenAI API using the bot's personality
bot_response = openAI(f"{BOT_PERSONALITY}{prompt}")
# Sending back response to telegram group
print(telegram_bot_sendtext(bot_response, chat_id, msg_id))
# Verifying that the user is responding to the ChatGPT bot
if 'reply_to_message' in result['message']:
if result['message']['reply_to_message']['from']['is_bot']:
prompt = result['message']['text']
bot_response = openAI(f"{BOT_PERSONALITY}{prompt}")
print(telegram_bot_sendtext(bot_response, chat_id, msg_id))
except Exception as e:
print(e)

# Updating file with last update ID
with open(filename, 'w') as f:
f.write(last_update)

return "done"


# 5 Running a check every 5 seconds to check for new messages
def main():
timertime = 5
Chatbot()
# 5 sec timer
threading.Timer(timertime, main).start()


# Run the main function
if __name__ == "__main__":
main()

作者

Yan Tao

发布于

2023-02-21

更新于

2023-02-21

许可协议

评论